﻿using System.Collections;
using System.IO;

namespace FileListView
{
    /// <summary>
    /// Used to keep track of file locations loaded by a FileListView object
    /// </summary>
    public sealed class NavigationHistory
    {
        private ArrayList m_DirectoryHistory = new ArrayList(5);
        private int m_HistoryLocation;

        /// <summary>
        /// Accesses the path that is currently being pointed to in the history list
        /// </summary>
        public string CurrentLocation
        {
            get;
            set;
        }

        /// <summary>
        /// Used to determine if we can go backward in the history list
        /// </summary>
        /// <returns>true if able to navigate backward, false otherwise</returns>
        public bool CanGoBack()
        {
            if (m_HistoryLocation == 0)
            {
                return false;
            }
                return true;
        }

        /// <summary>
        /// Used to determine if we can move forward in the history list
        /// </summary>
        /// <returns>true if able to navigate forwward, false otherwise</returns>
        public bool CanGoForward()
        {
            if ((m_HistoryLocation + 1) > (m_DirectoryHistory.Count - 1))
            {
                return false;
            }
                return true;
        }

        /// <summary>
        /// Determines if we can go one directory "up" from the current location
        /// </summary>
        /// <returns>true if able to move "up" to a valid directory, false otherwise</returns>
        public bool CanGoUp()
        {
            string currentDirectory = m_DirectoryHistory[m_HistoryLocation].ToString();

            string temp = Path.GetPathRoot(currentDirectory);
            if (temp.CompareTo(currentDirectory) == 0)
            {
                return false;
            }
                return true;
        }

        /// <summary>
        /// Sets the location pointer back 1 position in the history list
        /// </summary>
        /// <returns>Path being pointed to by the location pointer</returns>
        public string GoBack()
        {
            if (CanGoBack())
            {
                m_HistoryLocation -= 1;
                CurrentLocation = m_DirectoryHistory[m_HistoryLocation].ToString();
                return CurrentLocation;
            }
                return null;
        }

        /// <summary>
        /// Sets the location pointer forward 1 position in the history list
        /// </summary>
        /// <returns>Path being pointed to by the location pointer</returns>
        public string GoForward()
        {
            if (CanGoForward())
            {
                m_HistoryLocation += 1;
                CurrentLocation = m_DirectoryHistory[m_HistoryLocation].ToString();
                return CurrentLocation;
            }
                return null;
        }

        /// <summary>
        /// Gets the next directory "up" from the path being pointed to
        /// </summary>
        /// <returns>path to directory above current location</returns>
        public string GoUp()
        {
            if (CanGoUp())
            {
                string upPath = Path.GetDirectoryName(CurrentLocation);
                return upPath;
            }
            return null;
        }

        /// <summary>
        /// Adds a new item to the history list
        /// </summary>
        /// <param name="path">Path to add to list</param>
        public void Add(string path)
        {
            m_DirectoryHistory.Add(path);
            m_HistoryLocation = m_DirectoryHistory.Count - 1;
            CurrentLocation = m_DirectoryHistory[m_HistoryLocation].ToString();
        }


    }
}
